<template>
  <div class="app-container" >
   <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="参数简称" prop="param_en">
        <el-input
          v-model="queryParams.param_en"
          placeholder="请输入参数简称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="参数名称" prop="param_name">
        <el-input
          v-model="queryParams.param_name"
          placeholder="请输入参数名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="参数类型" prop="param_type">
          <el-select size="small" v-model="queryParams.param_type" placeholder="请选参数类型" clearable>
            <el-option
              v-for="dict in dict.type.sys_monitor_gas"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nssc:paramtype:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nssc:paramtype:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nssc:paramtype:remove']"
        >删除</el-button>
      </el-col>
   <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!-- 数据列表区域-->
    <el-table v-loading="loading" :data="paramTypeList" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" type="index" align="center" prop="id" width="60" />-->
      <el-table-column label="参数简称" align="center" prop="param_en"/>
      <el-table-column label="参数名称" align="center" prop="param_name"/>
      <el-table-column label="参数类型" align="center" prop="param_type">
      <template slot-scope="scope">
        <el-tag v-if="scope.row.param_type==0">正常气体</el-tag>
        <el-tag v-if="scope.row.param_type==1">有害气体</el-tag>
        <el-tag v-if="scope.row.param_type==2">刺激气体</el-tag>
        <el-tag v-if="scope.row.param_type==3">其他</el-tag>
      </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="creater_time" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creater_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nssc:paramtype:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nssc:paramtype:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
   <!--列表分页区域-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px"  @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数简称" prop="param_en">
              <el-select id="paramenname" v-model="form.param_en" placeholder="请选参数简称" @change="getOptioncslx" clearable>
                <el-option
                  v-for="item in paramen"
                  :key="item.param_en"
                  :label="item.param_en_name"
                  :value="item.param_en"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="display: none">
            <el-form-item label="参数名称" prop="param_name">
              <el-input id ="paramname" v-model="form.param_name" clearable placeholder="请输入参数名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参数类型">
              <el-select v-model="form.param_type" placeholder="请选参数类型">
                <el-option
                  v-for="dict in dict.type.sys_monitor_gas"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容(0-255)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" id="paramtypeid">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import "@/assets/styles/robt-bg.css";
import {
  listParamType,
  getParamType,
  delParamType,
  addParamType,
  updateParamType,
  listParamTypes
} from "@/api/nssc/paramtype/paramtype";
import $ from "jquery";
export default {
  dicts: ['sys_monitor_gas'],
  data() {
    return {
      sys_monitor_gas:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      param_names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数类型列表
      paramTypeList: [],
      //类型列表
      typeList: [],
      //参数简称类型
      paramen: [
        {param_en:"DCDL",param_en_name:"电池电量"},
        {param_en:"XCWD",param_en_name:"现场温度"},
        {param_en:"XCSD",param_en_name:"现场湿度"},
        {param_en:"DJWD",param_en_name:"电机温度"}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        param_type: undefined,
        param_name: undefined,
        param_en: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        param_name: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ] ,
        param_en: [
          { required: true, message: "参数简称不能为空", trigger: "blur" }
        ],
        param_type: [
          { required: true, message: "参数类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //自定义对话框关闭方法
    handleCancle () {
      $("#paramtypeid").css("display","");
      this.queryParams.param_en = undefined;
      this.queryParams.param_type = undefined;
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    //根据参数类型、查询是否有已创建
    getOptioncslx(param_en) {
      //根据下拉列表简称，获取对应中文名称存于文本框
      $("#paramname").val(null);
      const paramens = this.paramen;
      for(let i = 0; i < paramens.length; i++){
         if(paramens[i].param_en == param_en){
           $("#paramname").val(paramens[i].param_en_name);
           this.form.param_name = paramens[i].param_en_name;
           break;
         }
      }
      //判断是否已创建当前参数方法
      this.queryParams.param_en = param_en;
      listParamTypes(this.queryParams).then(response => {
        if(typeof(response)!="undefined" && response != null && response.rows.length>0) {
          $("#paramtypeid").css("display","none");
          this.$modal.confirm1('当前参数类型已创建！');
        }else {
          $("#paramtypeid").css("display","");
        }
      });
      this.queryParams.param_en = null;
    },
    /** 查询参数类型列表 */
    getList() {
      this.loading = true;
      listParamType(this.queryParams).then(response => {
        this.paramTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        param_en: undefined,
        param_name: undefined,
        param_type: undefined,
        creater_id:undefined,
        creater_time:undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.param_names = selection.map(item => item.param_name)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getParamType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateParamType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParamType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const param_names =row.param_name || this.param_names
      this.$modal.confirm('是否确认删除参数名称为"' + param_names + '"的数据项？').then(function() {
      // this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delParamType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
